<?php
namespace Admin\Controller;

class FundController extends \Admin\Controller\AdminController
{
	/**
	 * 基金分类管理
	 * @author 尹雷
	 */
	public function fundList()
	{
		$where['status'] = array('egt', 0);
	
		//查询条件
		if(I('condition')) {
			$con = I('condition');
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else {
				$where['fund_name'] = array('like', '%'.$con.'%');
			}
		} else if(I('trader_id')) {
			$where['trader_id'] = I('trader_id');
		}
	
		//获取基金信息
		$field = 'id,fund_name,fund_type,fund_trusteeship,fund_admin,expected_rate,trader_id,trader_name,comment_count,praise_count,status,create_time,update_time';
		$list = $this->lists('Fund', $where, '', $field);
	
		//查询结果不为空
		if(!empty($list)) {
			int_to_string($list);
			
			foreach($list as $key => $val) {
				$list[$key]['fund_type_text'] = C('FUND_TYPE')[$val['fund_type']];
				//计算好评率
				if($val['comment_count'] == 0) {
					$list[$key]['comment_rate'] = 100;
				} else {
					$list[$key]['comment_rate'] = bcdiv($val['praise_count'], $val['comment_count'], 4)*100;
				}
			}
		}
	
		//die(json_encode($list));
		$this->assign('_title', '基金信息列表');
		$this->assign('_list', $list);
		$this->display('fund_list');
	}
	
	/**
	 * 添加基金产品
	 * @author 尹雷
	 */
	public function addFund()
	{
		if(IS_GET) {
			//查询所有分类信息，只支持两级分类
			$category = M('Categories')->where(array('status' => 1))->field('id,name,parent_id')->select();
			!$category && $this->error('分类信息获取失败');
			
			//查询基金经理信息
			$trader = M('Trader')->where(array('status' => 1))->field('id,name')->select();
			!$trader && $this->error('基金经理信息获取失败');
			
			//查询合同信息
			$contract = M('Contract')->where(array('status' => 1))->field('id,name')->select();
			!$contract && $this->error('合同信息获取失败');
			
			$this->assign('categoryList', $category);
			$this->assign('contractList', $contract);
			$this->assign('traderList', $trader);
			$this->assign('stdUnit', C('CLOSE_DATE_UNIT'));
			$this->assign('_title', '新增基金产品');
			$this->display('add_edit');
		}
	
		//添加基金产口
		$res = D('Fund')->addFund();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('添加成功', U('Fund/fundList'));
	}
	
	/**
	 * 编辑基金产品
	 * @author 尹雷
	 */
	public function editFund()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('请选择要编辑的基金产品');
		
		if(IS_GET) {
			//查询基金产品
			$fund = M('Fund')->where(array('id' => $id))->find();
			!$fund && $this->error('基金产品查询失效');
			//查询所有分类信息，只支持两级分类
			$category = M('Categories')->where(array('status' => 1))->field('id,name,parent_id')->select();
			!$category && $this->error('分类信息获取失败');
				
			//查询基金经理信息
			$trader = M('Trader')->where(array('status' => 1))->field('id,name')->select();
			!$trader && $this->error('基金经理信息获取失败');
			
			//查询合同信息
			$contract = M('Contract')->where(array('status' => 1))->field('id,name')->select();
			!$contract && $this->error('合同信息获取失败');
			
			$fund['estb_time_format'] = date('Y-m-d', $fund['estb_time']);
			if($fund['list_time']) {
				$fund['list_time_format'] = date('Y-m-d', $fund['list_time']);
			}
			$fund['open_start_time_format'] = date('Y-m-d', $fund['open_start_time']);
			$fund['open_back_time_format'] = date('Y-m-d', $fund['open_back_time']);
			$fund['open_end_time_format'] = date('Y-m-d', $fund['open_end_time']);
			$fund['trade_time_format'] = date('Y-m-d', $fund['trade_time']);
			
			$this->assign('fund', $fund);
			$this->assign('categoryList', $category);
			$this->assign('contractList', $contract);
			$this->assign('traderList', $trader);
			$this->assign('stdUnit', C('CLOSE_DATE_UNIT'));
			$this->assign('_title', '编辑基金产品');
			$this->display('add_edit');
		}
	
		//添加基金产口
		$res = D('Fund')->editFund();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('添加成功', U('Fund/fundList'));
	}
	
	/**
	 * 基金详情
	 * @author 尹雷
	 */
	public function fundDetail()
	{
		$id = I('id', '');
		!$id && $this->error('请选择要查看的基金产品');
		
		//查询基金产品信息
		$fund = $this->returnOne('Fund', array('id' => $id, 'status' => array('egt', 0)));
		!$fund && $this->error('基金产品查询失败');
		
		//查询基金分类信息
		$cats = $this->returnList('FundCategories', array('fund_id' => $fund['id']));
		!$cats && $this->error('基金分类息查询失败');
		
		//查询基金净值信息
		$netvalue = $this->returnList('FundNetvalue', array('fund_id' => $fund['id']));
		if($netvalue === false) $this->error('基金净值息查询失败'); 
		
		$fund['is_recommend_text'] = C('FUND_RECOMMEND')[$fund['is_recommend']];
		$fund['section_code_text'] = C('FUND_SECTION_CODE')[$fund['section_code']];
		$fund['fund_type_text'] = C('FUND_TYPE')[$fund['fund_type']];
		$fund['std_time_text'] = $fund['std_time'] . ' ' . C('CLOSE_DATE_UNIT')[$fund['std_unit']];
		if($fund['trade_time']) $fund['trade_time_format'] = date('Y-m-d', $fund['trade_time']);
		$fund = array($fund);
		int_to_string($fund);
		
		$this->assign('fund', $fund[0]);
		$this->assign('_title', '基金产品详情');
		$this->assign('cats', $cats);
		$this->assign('netvalue', $netvalue);
		$this->display('fund_detail');
	}
	
	/**
	 * TODO:禁用的业务逻辑待定
	 * 更新基金状态
	 * @author 尹雷
	 */
	public function changeStatus($method=null)
	{
			$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);
	
		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('Fund', $map );
				break;
			case 'resume':
				$this->resume('Fund', $map );
				break;
			case 'delete':
				$this->delete('Fund', $map );
				break;
			default:
				$this->error('参数非法');
		}
		
	}
	
	/**
	 * 基金分类管理
	 * @author 尹雷
	 */
	public function fundCatList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
		
		//查询条件
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else {
				$where['name'] = array('like', '%'.$con.'%');
			}
		}
		
		//获取基金分类信息
		$list = $this->lists('Categories', $where, 'status desc');
		
		//如果查询结果不为空
		if(!empty($list)) {
			int_to_string($list);
			
			//获取分组分类id
			$ids = array();
			foreach($list as $key => $val) {
				if($val['parent_id'] != -1) {
					$ids[] = $val['parent_id'];
				}
			}
			
			//查询分组分类信息
			$ids = array_unique($ids);
			if(!empty($ids)) {
				$parentList = M('Categories')->where(array('id' => array('in', $ids)))->select();
			} else {
				$parentList = array();
			}
			
			foreach($list as $key => $val) {
				if($val['parent_id'] != -1) {
					foreach ($parentList as $k => $v) {
						if($val['parent_id'] === $v['id']) {
							$list[$key]['parent_name'] = $v['name'];
							break;
						}
					}
				} else {
					$list[$key]['parent_name'] = '顶级分类';
				}
			}
		}
		
		//die(json_encode($list));
		$this->assign('_title', '基金分类信息');
		$this->assign('_list', $list);
		$this->display('cat_list');
	}
	
	/**
	 * 添加基金分类
	 * @author 尹雷
	 */
	public function addFundCat()
	{
		if(IS_GET) {
			//查询所有分类信息，只支持两级分类
			$list = M('Categories')->where(array('parent_id' => -1, 'status' => 1))->select();
			if($list === false) $this->error('分类信息获取失败');
			
			$this->assign('list', $list);
			$this->assign('_title', '新增基金分类');
			$this->display('add_edit_cat');
		}
	
		//添加基金分类
		$res = D('Categories')->addFundCat();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('添加成功', U('Fund/fundCatList'));
	}
	
	/**
	 * 编辑基金分类
	 * @author 尹雷
	 */
	public function editFundCat()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('请选择要编辑的基金分类');
	
		if(IS_GET) {
			//查询所有分类信息，只支持两级分类
			$list = M('Categories')->where(array('parent_id' => -1, 'status' => 1))->select();
			if($list === false) $this->error('分类信息获取失败');
			$category = M('Categories')->where(array('id' => $id))->find();
			
			$this->assign('list', $list);
			$this->assign('_title', '编辑基金分类');
			$this->assign('category', $category);
			$this->display('add_edit_cat');
		}
	
		//编辑基金分类
		$res = D('Categories')->editFundCat();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('编辑成功', U('Fund/fundCatList'));
	}
	
	/**
	 * TODO:禁用的业务逻辑待定
	 * 更新基金分类状态
	 * @author 尹雷
	 */
	public function changeCatStatus($method=null)
	{
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);

		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('Categories', $map );
				break;
			case 'resume':
				$this->resume('Categories', $map );
				break;
			case 'delete':
				$this->delete('Categories', $map );
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * 查询基金分类信息
	 * @author 尹雷
	 */
	public function searchCat()
	{
		$where = array();
		
		if(I('parent_id', '', '/^[-1-9]\d*$/')) {
			$where['parent_id'] = I('parent_id');
		}
		
		if(I('id', '', '/^[1-9]\d*$/')) {
			$where['id'] = I('id');
		}
		
		$where['status'] = I('status', 1, '/^\d$/');
		$res = M('Categories')->where($where)->field('id,name,parent_id')->select();
		
		//查询失败
		!$res && $this->ajaxReturn(array('error' => 1, 'msg' => '查询失败'));
		//查询成功
		$this->ajaxReturn(array('error' => 0, 'msg' => $res));
	}
	
	/**
	 * 查询基金产品所属的分类信息；
	 */
	public function fundCatRelation() 
	{
		$where = array();
		
		//根据fund_id查询
		$fundId = I('fund_id', '', '/^\d+$/');
		if($fundId) {
			$where['fund_id'] = $fundId;
		}

		//根据cat_id查询
		$catId = I('cat_id', '', '/^\d+$/');
		if($catId) {
			$where['cat_id'] = $catId;
		}
		
		//判断是否传递参数
		if(empty($where)) {
			$data['error'] = 1;
			$data['msg'] = '参数错误';
			$this->ajaxReturn($data);
		}
		
		//查询基金分类信息
		$where['status'] = 1;
		$res = M('FundCategories')->where($where)->select();
		!$res && $this->ajaxReturn(array('error' => 1, 'msg' => '查询失败'));
		
		//获取分类id
		$catIds = array();
		foreach ($res as $val) {
			$catIds[] = $val['cat_id'];
		}
		
		//查询分类信息
		$cats = M('Categories')->where(array('id' => array('in', $catIds), 'status' => 1))->select();
		!$cats && $this->ajaxReturn(array('error' => 1, 'msg' => '查询失败1'));

		foreach ($res as $key => &$val) {
			foreach ($cats as $cat) {
				if($val['cat_id'] == $cat['id']) {
					$val['cat_parent_id'] = $cat['parent_id'];
					break;
				}
			}
		}
		
		$data = array('error' => 0, 'msg' => $res);
		$this->ajaxReturn($data);
	}
	
	/**
	 * 查询基金净值
	 * @author 尹雷
	 */
	public function netvalueList()
	{
		//查询基金列表
		$fund = M('Fund')->where('status=1')->order('sort_order')->field('id,fund_name,trader_id,trader_name')->select();
		!$fund && $this->error('基金列表查询失败');
		
		//根据fund_id查询
		$fundId = I('fund_id', '', '/^\d+$/');
//		if(!$fundId) {
//			$fundId = reset($fund)['id'];
//		}
		
		//查询净值信息
		if(!empty($fundId)){
			$where['fund_id'] = $fundId;
		}
		$where['status'] = array('egt', 0);
		$list = $this->lists('FundNetvalue', $where, 'create_time desc');
		
		int_to_string($list);
		$this->assign('selected', $fundId);
		$this->assign('_title', '基金净值列表');
		$this->assign('fund', $fund);
		$this->assign('_list', $list);
		$this->display('netvalue_list');
	}
	
	/**
	 * 更新基金净值状态
	 * @author 尹雷
	 */
	public function changeNetValueStatus($method=null)
	{
		!$method && $this->error('参数错误');
		if($method=="delete"){
			$id = array_unique((array)I('id',0));
			$id = is_array($id) ? implode(',',$id) : $id;
			$map['id'] =   array('in',$id);
			M("FundNetvalue")->where($map)->delete();
		}
		$this->changeRecodeStatus($method, 'FundNetvalue');
	}
	
	/**
	 * 添加基金产品净值
	 * @author 尹雷
	 */
	public function addNetvalue()
	{
		//根据fund_id查询
		$fundId = I('fund_id', '', '/^\d+$/');
		!$fundId && $this->error('参数错误');
		
		if(IS_GET) {
			//查询基金产品信息
			$fund = $this->returnOne('Fund',array('id' => $fundId), '', 'id,fund_name,trader_id,trader_name');
			!$fund && $this->error('查询失败');
			$this->assign('_title', '新增基金产品净值信息');
			$this->assign('fund', $fund);
			$this->display('add_edit_netvalue');
		}
		//添加基金产口
		$res = D('FundNetvalue')->addNetvalue();
		if($res['error'] != 0) $this->error($res['msg']);

		$this->ajaxReturn(array("info"=>"添加成功"));
		//$this->success('添加成功', U('Fund/netvalueList', array('fund_id' => $fundId)));
	}
	
	/**
	 * 编辑基金产品净值
	 * @author 尹雷
	 */
	public function editNetvalue()
	{
		//根据fund_id查询
		$fundId = I('fund_id', '', '/^\d+$/');
		!$fundId && $this->error('参数错误1');
	
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('参数错误2');
		
		if(IS_GET) {
			//查询基金产品信息
			$fund = $this->returnOne('Fund',array('id' => $fundId), '', 'id,fund_name,trader_id,trader_name');
			!$fund && $this->error('查询失败');
			//查询净值信息
			$netvalue = $this->returnOne('FundNetvalue',array('id' => $id), '');
			!$netvalue && $this->error('净值查询失败');
			$netvalue['netvalue_time_format'] = date('Y-m-d H:i:s', $netvalue['netvalue_time']);
			$this->assign('_title', '编辑基金产品净值信息');
			$this->assign('fund', $fund);
			$this->assign('netvalue', $netvalue);
			$this->display('add_edit_netvalue');
		}
	
		//添加基金产口
		$res = D('FundNetvalue')->editNetvalue();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->ajaxReturn(array("info"=>"修改成功"));
		//$this->success('编辑成功', U('Fund/netvalueList', array('fund_id' => $fundId)));
	}
	
	/**
	 * 基金产品净值详情
	 * @author 尹雷
	 */
	public function netvalueDetail()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('参数错误');
	
		//查询净值信息
		$netvalue = $this->returnOne('FundNetvalue',array('id' => $id), '');
		!$netvalue && $this->error('净值查询失败');
		$netvalue['netvalue_time_format'] = date('Y-m-d H:i:s', $netvalue['netvalue_time']);
		
		//查询基金经理信息
		$trader = $this->returnOne('Fund', array('id' => $netvalue['fund_id']), '', 'trader_id,trader_name');
		!$trader && $this->error('基金经理查询失败');
		
		//查询日志
		$log = $this->returnList('FundNetvalueLog', array('netvalue_id' => $id));
		!$log && $this->error('净值日志查询失败');
		
		$netvalue = array($netvalue);
		int_to_string($netvalue);
		$this->assign('_title', '基金产品净值详情信息');
		$this->assign('log', $log);
		$this->assign('netvalue', $netvalue[0]);
		$this->assign('trader', $trader);
		$this->display('netvalue_detail');
	}
}